package com.zhiche.lisa.bms.controller.receipt;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.receipt.ReceiptAffirm;
import com.zhiche.lisa.bms.dao.model.receipt.ReceiptAffirmDtl;
import com.zhiche.lisa.bms.pojo.vo.receipt.ReceiptAffirmAndDtlVO;
import com.zhiche.lisa.bms.pojo.vo.receipt.ReceiptAffirmDtlAndInvoiceVO;
import com.zhiche.lisa.bms.pojo.vo.receipt.ReceiptAffirmVo;
import com.zhiche.lisa.bms.service.receipt.ReceiptAffirmService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收款确认 前端控制器
 * </p>
 *
 * @author hongbo
 * @since 2018-09-27
 */
@RestController
@RequestMapping("/receiptAffirm")
@Api(value = "/receiptAffirm", description = "收款确认管理接口", tags = {"收款确认管理接口"})
public class ReceiptAffirmController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReceiptAffirmController.class);
    @Autowired
    private ReceiptAffirmService receiptAffirmService;

    /**
     * 收款确认分页查询
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/qureyReceiptAffirmLinePage")
    @ApiOperation(value = "收款确认分页查询", notes = "收款确认分页查询", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Page<ReceiptAffirmVo>> qureyReceiptAffirmLinePage(@RequestBody Page<ReceiptAffirm> page) {
        LOGGER.info("ReceiptAffirmController.qureyReceiptAffirmLinePage param: {}", page);
        RestfulResponse<Page<ReceiptAffirmVo>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(receiptAffirmService.qureyReceiptAffirmLinePage(page));
        } catch (BaseException e) {
            LOGGER.error("ReceiptAffirmController.qureyReceiptAffirmLinePage error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ReceiptAffirmController.qureyReceiptAffirmLinePage error:{}", e.getMessage());
            result.setMessage("收款确认分页查询异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 收款确认批量删除
     *
     * @param ids
     */
    @PostMapping(value = "/deleteReceiptAffirmById")
    @ApiOperation(value = "收款确认批量删除", notes = "收款确认批量删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<String> deleteReceiptAffirmById(@RequestBody List<Long> ids) {
        LOGGER.info("ReceiptAffirmController.deleteReceiptAffirmById param: {}", ids);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            receiptAffirmService.deleteReceiptAffirmById(ids);
        } catch (BaseException e) {
            LOGGER.error("ReceiptAffirmController.deleteReceiptAffirmById error {}", e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            LOGGER.error("ReceiptAffirmController.deleteReceiptAffirmById error {}", e.getMessage());
            result.setMessage("收款确认批量删除异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    /**
     * 新增收款确认
     *
     * @param receiptAffirmVO
     */
    @PostMapping(value = "/insertReceiptAffirm")
    @ApiOperation(value = "新增收款确认", notes = "新增收款确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<String> insertReceiptAffirm(@RequestBody ReceiptAffirmAndDtlVO receiptAffirmVO) {
        LOGGER.info("ReceiptAffirmController.insertReceiptAffirm params: {}", receiptAffirmVO);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            ReceiptAffirm receiptAffirm = receiptAffirmVO.getReceiptAffirm();
            List<ReceiptAffirmDtl> receiptAffirmDtls = receiptAffirmVO.getReceiptAffirmDtls();
            receiptAffirmService.insertReceiptAffirm(receiptAffirm, receiptAffirmDtls,true);
        } catch (BaseException e) {
            LOGGER.error("ReceiptAffirmController.insertReceiptAffirm error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ReceiptAffirmController.insertReceiptAffirm error:{}", e.getMessage());
            result.setMessage("新增收款确认异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @GetMapping(value = "/selectReceiptAndDtlById/{id}")
    @ApiOperation(value = "获取收款确认及明细", notes = "获取收款确认及明细", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<ReceiptAffirmAndDtlVO> selectReceiptAndDtlById(@PathVariable("id") Long id) {
        LOGGER.info("ReceiptAffirmController.selectReceiptAndDtlById params: {}", id);
        RestfulResponse<ReceiptAffirmAndDtlVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(receiptAffirmService.selectReceiptAndDtlById(id));
        } catch (BaseException e) {
            LOGGER.error("ReceiptAffirmController.selectReceiptAndDtlById error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ReceiptAffirmController.selectReceiptAndDtlById error:{}", e.getMessage());
            result.setMessage("新增收款确认异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/updateReceiptAffirmAndDtl")
    @ApiOperation(value = "修改收款确认及明细", notes = "修改收款确认及明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<String> updateReceiptAffirmAndDtl(@RequestBody ReceiptAffirmAndDtlVO receiptAffirmAndDtlVO) {
        LOGGER.info("ReceiptAffirmController.updateReceiptAffirmAndDtl params: {}", receiptAffirmAndDtlVO);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            receiptAffirmService.updateReceiptAffirmAndDtl(receiptAffirmAndDtlVO.getReceiptAffirm(), receiptAffirmAndDtlVO.getReceiptAffirmDtls());
        } catch (BaseException e) {
            LOGGER.error("ReceiptAffirmController.updateReceiptAffirmAndDtl error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("ReceiptAffirmController.updateReceiptAffirmAndDtl error:{}", e.getMessage());
            result.setMessage("修改收款确认及明细异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}

